import {isString} from "../../utils/string/isString";

export default class Animation extends PIXI.Container {

    constructor(symbolName, atlas) {
        super();
        this._symbol = new Symbol(atlas.getSymbolData(symbolName), atlas);
        this._symbol.update();
        this.addChild(this._symbol);

        this.onFrameChanged = this.onFrameChanged.bind(this);
        this._behavior = new MovieBehavior(this, this.onFrameChanged, atlas.frameRate);
        this._behavior.numFrames = this._symbol.numFrames;
        this._behavior.on('complete', this.onComplete);
        this.play();
    }

    get currentLabel() {
        return this._symbol.currentLabel;
    }

    get currentFrame() {
        return this._behavior.currentFrame;
    }

    set currentFrame(value) {
        this._behavior.currentFrame = value;
    }

    get currentTime() {
        return this._behavior.currentTime;
    }

    set currentTime(value) {
        this._behavior.currentTime = value;
    }

    /**
     * 获取动画播放的帧速率
     * @returns {number}
     */
    get frameRate() {
        return this._behavior.frameRate;
    }

    /**
     * 设置动画播放的帧速率
     * @param value
     */
    set frameRate(value) {
        this._behavior.frameRate = value;
    }

    /**
     * 获取动画是否循环播放
     * @returns {boolean}
     */
    get loop() {
        return this._behavior.loop;
    }

    /**
     * 设置动画是否循环播放
     * @param value
     */
    set loop(value) {
        this._behavior.loop = value;
    }

    /**
     * 动画时间线中的帧数
     * @returns {number}
     */
    get numFrames() {
        return this._behavior.numFrames;
    }

    /**
     * 是否正在播放
     * @returns {boolean}
     */
    get isPlaying() {
        return this._behavior.isPlaying;
    }

    /**
     * 以秒为单位的动画的总长度
     * @returns {number}
     */
    get totalTime() {
        return this._behavior.totalTime;
    }

    getNextLabel(afterLabel = null) {
        return this._symbol.getNextLabel(afterLabel);
    }

    getFrame(label) {
        return this._symbol.getFrame(label);
    }

    onComplete() {
        this.emit('complete');
    }

    onFrameChanged(frameIndex) {
        this._symbol.currentFrame = frameIndex;
    }

    play() {
        this._behavior.play();
    }

    pause() {
        this._behavior.pause();
    }

    stop() {
        this._behavior.stop();
    }

    gotoFrame(indexOrLabel) {
        this.currentFrame = isString(indexOrLabel)
            ? this._symbol.getFrame(indexOrLabel)
            : parseInt(indexOrLabel, 10);
    }

    addFrameAction(indexOrLabel, action) {
        let frameIndex = isString(indexOrLabel)
            ? this._symbol.getFrame(indexOrLabel)
            : parseInt(indexOrLabel, 10);

        this._behavior.addFrameAction(frameIndex, action);
    }

    removeFrameAction(indexOrLabel, action) {
        let frameIndex = isString(indexOrLabel)
            ? this._symbol.getFrame(indexOrLabel)
            : parseInt(indexOrLabel, 10);

        this._behavior.removeFrameAction(frameIndex, action);
    }

    removeFrameActions(indexOrLabel) {
        let frameIndex = isString(indexOrLabel)
            ? this._symbol.getFrame(indexOrLabel)
            : parseInt(indexOrLabel, 10);

        this._behavior.removeFrameActions(frameIndex);
    }

    advanceTime(time) {
        let frameRate = this._behavior.frameRate;
        let prevTime = this._cumulatedTime;

        this._behavior.advanceTime(time);
        this._cumulatedTime += time;

        if (parseInt(prevTime * frameRate, 10) != parseInt(this._cumulatedTime * frameRate, 10)) {
            this._symbol.nextFrame_MovieClips();
        }
    }
}